package com.xzx.youku;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = MainActivity.class.getName();

    private RelativeLayout level1;
    private RelativeLayout level2;
    private RelativeLayout level3;
    private ImageView icon_home;
    private ImageView icon_menu;

    //是否显示一级菜单，默认显示
    private boolean isShowLevel1 = true;
    //是否显示二级菜单，默认显示
    private boolean isShowLevel2 = true;
    //是否显示三级菜单，默认显示
    private boolean isShowLevel3 = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        level1 = (RelativeLayout) findViewById(R.id.level1);
        level2 = (RelativeLayout) findViewById(R.id.level2);
        level3 = (RelativeLayout) findViewById(R.id.level3);
        icon_home = (ImageView) findViewById(R.id.icon_home);
        icon_menu = (ImageView) findViewById(R.id.icon_menu);

        MyClickListener listener = new MyClickListener();
        icon_home.setOnClickListener(listener);
        icon_menu.setOnClickListener(listener);
    }

    class MyClickListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.icon_home:
                    //home菜单控制三级菜单的隐藏，二级菜单的显示和隐藏
                    if (isShowLevel2) {
                        //隐藏二级菜单
                        isShowLevel2 = false;
                        Tools.hideView(level2);
                        if (isShowLevel3) {
                            //如果三级菜单显示着一并隐藏掉
                            isShowLevel3 = false;
                            Tools.hideView(level3, 200);
                        }
                    } else {
                        //显示二级菜单
                        isShowLevel2 = true;
                        Tools.showView(level2);
                    }
                    break;
                case R.id.icon_menu:
                    //菜单图标控制三级菜单的显示与隐藏
                    if (isShowLevel3) {
                        //隐藏
                        isShowLevel3 = false;
                        Tools.hideView(level3);
                    } else {
                        //显示
                        isShowLevel3 = true;
                        Tools.showView(level3);
                    }
                    break;
            }
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Toast.makeText(this, "hhh", Toast.LENGTH_LONG).show();
            if (isShowLevel1) {
                //隐藏
                isShowLevel1 = false;
                Tools.hideView(level1);
                if (isShowLevel2) {
                    isShowLevel2 = false;
                    Tools.hideView(level2, 200);
                    if (isShowLevel3) {
                        isShowLevel3 = false;
                        Tools.hideView(level3, 400);
                    }
                }
            } else {
                //显示
                isShowLevel1 = true;
                Tools.showView(level1);
                isShowLevel2 = true;
                Tools.showView(level2, 200);
            }
            return true;//返回true，表示已经处理了菜单按键的处理
        }
        return super.onKeyDown(keyCode, event);
    }
}
